package com.evangelion.core.daoImpl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Kellycasma
 */
@Component
public class ConnectionUtil extends AbstractDAO implements Serializable {

    private Connection connection;

    @Autowired
    private DataSource dataSource;

    private boolean isConect() throws SQLException {
        if (connection == null || connection.isClosed()) {
            return false;
        }
        return true;
    }

    public boolean connect() {
        try {
            if (!isConect()) {
                try {
//                    InitialContext initialContext = new InitialContext();
//                    Context envCtx = (Context) initialContext.lookup("java:comp/env");
//                    DataSource dataSource = (DataSource) envCtx.lookup("JNDUEVANGELION");
                    connection = dataSource.getConnection();
    // connection = em.unwrap(java.sql.Connection.class);

                    return true;
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }


//    public boolean connectWrap() {
//        try {
//            if (!isConect()) {
//                try {
//    // InitialContext initialContext = new InitialContext();
//    // DataSource dataSource = (DataSource) initialContext.lookup("jdbc/amsys");
//    // connection = dataSource.getConnection();
//                    System.out.println("EntityManager: " + em);
//                    connection = em.unwrap(Connection.class);
//
//                    return true;
//                } catch (Exception ex) {
//                    System.out.println(ex.getMessage());
//                }
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return false;
//    }
//
//    public boolean closeConect() {
//        try {
//            if (isConect()) {
//                try {
//                    connection.close();
//                    return true;
//                } catch (Exception ex) {
//                    System.out.println(ex.getMessage());
//                }
//            }
//        } catch (SQLException ex) {
//            Logger.getLogger(ConnectionUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return true;
//    }

    public synchronized Connection getConnection() {
        connect();
        return connection;
    }

    /**
     *
     * @return
     */
    public static ServletContext getServletContext() {
        return (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
    }
}